package com.sanjiahantang.youjiarole.module.programmeprice.adapters;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sanjiahantang.plugin.swipemenulistview.NoRollSwipeMenuListView;
import com.sanjiahantang.uilibrary.adapter.listview.BaseAdapterHelper;
import com.sanjiahantang.uilibrary.adapter.listview.QuickAdapter;
import com.sanjiahantang.uilibrary.utils.Utils;
import com.sanjiahantang.uilibrary.views.MyListView;
import com.sanjiahantang.uilibrary.views.TagGroup;
import com.sanjiahantang.youjiarole.R;
import com.sanjiahantang.youjiarole.base.BaseView;
import com.sanjiahantang.youjiarole.config.StaticValue;
import com.sanjiahantang.youjiarole.module.goods.views.impl.GoodsListActivity;
import com.sanjiahantang.youjiarole.module.programmeprice.beans.ProgramBean;
import com.sanjiahantang.youjiarole.module.programmeprice.views.ProgramPriceView;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import static com.sanjiahantang.youjiarole.config.StaticValue.CALLBACK_TYPE_BOOLEAN;
import static com.sanjiahantang.youjiarole.config.StaticValue.CALLBACK_TYPE_QUICK_ADAPTER;
import static com.sanjiahantang.youjiarole.config.StaticValue.KEY_TYPE_GOODSID;


/**
 * Created by zy on 2017/8/9.
 */

public class ProgramProjectAdapter extends RecyclerView.Adapter implements StaticValue{


    private final Context context;
    //布局标识集合
    private final List<Integer> typeList;
    private LayoutInflater inflater;
    private ProgramBean.Data data;
    private BaseView.OnLoadListener loadListener;
    public String sivisible_swich;


    public ProgramBean.Data getData() {
        return data;
    }

    public void setData(ProgramBean.Data data) {
        this.data = data;
    }

    public void addCell(Object item, int type) {
        switch (type) {
            case 2://配饰
                ProgramBean.Data.GoodsListBean accessoriesListBean = (ProgramBean.Data.GoodsListBean) item;
                int accessoryIndex = 0,max = data.getAccessoriesList().size();
                for (;accessoryIndex < max; accessoryIndex++) {
                    if (data.getAccessoriesList().get(accessoryIndex).getSkuFaId().contains(accessoriesListBean.getSkuFaId())) {
                        data.getAccessoriesList().get(accessoryIndex).setGoodsNum(accessoriesListBean.getGoodsNum() + data.getAccessoriesList().get(accessoryIndex).getGoodsNum());
                        data.getAccessoriesList().get(accessoryIndex).setGoodsCount(String.valueOf((data.getAccessoriesList().get(accessoryIndex).getGoodsNum()) * Float.valueOf(data.getAccessoriesList().get(accessoryIndex).getGoodsPrice())));
                        break;
                    }
                }
                if (accessoryIndex == max){
//                    if (data.getAccessoriesList()!=null&&data.getAccessoriesList().size()>0){
//                        accessoriesListBean.setPhone(data.getAccessoriesList().get(0).getPhone());
//                    }else {
//                        accessoriesListBean.setPhone("");
//                    }
                    data.getAccessoriesList().add(accessoriesListBean);
                }
                break;
            case 1://家居
                ProgramBean.Data.GoodsListBean furnitureListBean = (ProgramBean.Data.GoodsListBean) item;
                int funitureIndex = 0,maxfun = data.getFurnitureList().size();
                for (;funitureIndex < maxfun; funitureIndex++) {
                    if (data.getFurnitureList().get(funitureIndex).getSkuFaId().contains(furnitureListBean.getSkuFaId())) {
                        data.getFurnitureList().get(funitureIndex).setGoodsNum(furnitureListBean.getGoodsNum() + data.getFurnitureList().get(funitureIndex).getGoodsNum());
                        data.getFurnitureList().get(funitureIndex).setGoodsCount(String.valueOf((data.getFurnitureList().get(funitureIndex).getGoodsNum()) * Float.valueOf(data.getFurnitureList().get(funitureIndex).getGoodsPrice())));
                        break;
                    }
                }
                if (funitureIndex == maxfun){
//                    if (data.getFurnitureList()!=null&&data.getFurnitureList().size()>0){
//                        furnitureListBean.setPhone(data.getFurnitureList().get(0).getPhone());
//                    }else {
//                        furnitureListBean.setPhone("");
//                    }
                    data.getFurnitureList().add(furnitureListBean);
                }
                break;

            case 3://施工
                ProgramBean.Data.ServiceListBean serviceListBean = (ProgramBean.Data.ServiceListBean) item;
                int serviceIndex = 0,sermax = data.getServiceList().size();
                for (;serviceIndex < sermax; serviceIndex++) {
                    if (data.getServiceList().get(serviceIndex).getSkuFaId().contains(serviceListBean.getSkuFaId())) {
                        data.getServiceList().get(serviceIndex).setServiceNum(serviceListBean.getServiceNum() + data.getServiceList().get(serviceIndex).getServiceNum());
                        data.getServiceList().get(serviceIndex).setServiceCount(String.valueOf((data.getServiceList().get(serviceIndex).getServiceNum()) * Float.valueOf(data.getServiceList().get(serviceIndex).getServicePrice())));
                        break;
                    }
                }
                if (serviceIndex == sermax){
//                    if (data.getServiceList()!=null&&data.getServiceList().size()>0){
//                        serviceListBean.setPhone(data.getServiceList().get(0).getPhone());
//                    }else {
//                        serviceListBean.setPhone("");
//                    }
                    data.getServiceList().add(serviceListBean);
                }
                break;
            case 4://主材
                ProgramBean.Data.GoodsListBean materialListBean = (ProgramBean.Data.GoodsListBean) item;
                int mateiaIndex = 0,maxmateria = data.getMaterialList().size();
                for (;mateiaIndex < maxmateria; mateiaIndex++) {
                    if (data.getMaterialList().get(mateiaIndex).getSkuFaId().contains(materialListBean.getSkuFaId())) {
                        data.getMaterialList().get(mateiaIndex).setGoodsNum(materialListBean.getGoodsNum() + data.getMaterialList().get(mateiaIndex).getGoodsNum());
                        data.getMaterialList().get(mateiaIndex).setGoodsCount(String.valueOf((data.getMaterialList().get(mateiaIndex).getGoodsNum()) * Float.valueOf(data.getMaterialList().get(mateiaIndex).getGoodsPrice())));
                        break;
                    }
                }
                if (mateiaIndex == maxmateria){
//                    if (data.getMaterialList()!=null&&data.getMaterialList().size()>0){
//                        materialListBean.setPhone(data.getMaterialList().get(0).getPhone());
//                    }else {
//                        materialListBean.setPhone("");
//                    }
                    data.getMaterialList().add(materialListBean);
                }
                break;
        }
        notifyDataSetChanged();
    }

    public void setChangeData(String isoo) {
        sivisible_swich = isoo;
        if (ProgramPriceView.SWITCH_EDIT.equals(sivisible_swich)) {//编辑刷新
            notifyDataSetChanged();
        } else if (ProgramPriceView.SWITCH_SELECT_NONE.equals(sivisible_swich)) {//全不选

            for (int i = 0; i < ProgramProjectAdapter.this.data.getServiceList().size(); i++) {
                ProgramProjectAdapter.this.data.getServiceList().get(i).setIsRecommend(0);
            }
            for (int i = 0; i < ProgramProjectAdapter.this.data.getMaterialList().size(); i++) {
                ProgramProjectAdapter.this.data.getMaterialList().get(i).setIsRecommend(0);
            }

            for (int i = 0; i < ProgramProjectAdapter.this.data.getFurnitureList().size(); i++) {
                ProgramProjectAdapter.this.data.getFurnitureList().get(i).setIsRecommend(0);
            }
            for (int i = 0; i < ProgramProjectAdapter.this.data.getAccessoriesList().size(); i++) {
                ProgramProjectAdapter.this.data.getAccessoriesList().get(i).setIsRecommend(0);
            }
        } else if (ProgramPriceView.SWITCH_SELECT_ALL.equals(sivisible_swich)) {//全选
            for (int i = 0; i < ProgramProjectAdapter.this.data.getServiceList().size(); i++) {
                ProgramProjectAdapter.this.data.getServiceList().get(i).setIsRecommend(1);
            }
            for (int i = 0; i < ProgramProjectAdapter.this.data.getMaterialList().size(); i++) {
                ProgramProjectAdapter.this.data.getMaterialList().get(i).setIsRecommend(1);
            }

            for (int i = 0; i < ProgramProjectAdapter.this.data.getFurnitureList().size(); i++) {
                ProgramProjectAdapter.this.data.getFurnitureList().get(i).setIsRecommend(1);
            }
            for (int i = 0; i < ProgramProjectAdapter.this.data.getAccessoriesList().size(); i++) {
                ProgramProjectAdapter.this.data.getAccessoriesList().get(i).setIsRecommend(1);
            }

        }
        notifyDataSetChanged();
        TotalMoney();

    }


    public ProgramProjectAdapter(Context context, BaseView.OnLoadListener listener) {
        this.context = context;
        this.typeList = new ArrayList<>();
        typeList.add(ProgramPriceView.TYPE_SERVICE);
        typeList.add(ProgramPriceView.TYPE_MATERIAL);
        typeList.add(ProgramPriceView.TYPE_FURNITURE);
        typeList.add(ProgramPriceView.TYPE_ACCESSORIES);
        this.loadListener = listener;
        this.inflater = LayoutInflater.from(context);
    }

    @Override
    public ProgramHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        int resId = -1;
        switch (viewType) {
            case ProgramPriceView.TYPE_SERVICE:
                resId = R.layout.item_program_nopic;
                break;
            case ProgramPriceView.TYPE_MATERIAL:
                resId = R.layout.item_program_nopic_swiplist;
                break;
            case ProgramPriceView.TYPE_FURNITURE:
                resId = R.layout.item_program_nopic_swiplist;
                break;
            case ProgramPriceView.TYPE_ACCESSORIES:
                resId = R.layout.item_program_nopic_swiplist;
                break;

        }
        return new ProgramHolder(inflater.inflate(resId, null), data, viewType);

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ((ProgramHolder) holder).bindData(data, typeList.get(position));
    }

    @Override
    public int getItemViewType(int position) {
        return typeList.get(position);
    }

    @Override
    public int getItemCount() {
        return typeList.size();
    }

    private class ProgramHolder extends RecyclerView.ViewHolder {

        public ProgramHolder(View itemView, ProgramBean.Data data, int position) {
            super(itemView);
            bindData(data, position);
        }
        private void bindData(ProgramBean.Data data, int position) {

            if (position == ProgramPriceView.TYPE_SERVICE) {
                //施工列表
                initService(itemView, data.getServiceList());
            }
            if (position == ProgramPriceView.TYPE_MATERIAL) {
                //主材列表
                initGoods(itemView,0,"主材", data.getMaterialList());
            }

            if (position == ProgramPriceView.TYPE_FURNITURE) {
                initGoods(itemView,1,"家具", data.getFurnitureList());
            }

            if (position == ProgramPriceView.TYPE_ACCESSORIES) {
                initGoods(itemView,2,"配饰", data.getAccessoriesList());
            }

        }

        //施工
        public void initService(View itemView, final List<ProgramBean.Data.ServiceListBean> sgData) {
            final  CheckBox sg_checkbox = (CheckBox) itemView.findViewById(R.id.iv_titleIcon);
            final MyListView goodListView = (MyListView) itemView.findViewById(R.id.SG_list);
            TextView txtGoodsCount = (TextView) itemView.findViewById(R.id.comName);
            txtGoodsCount.setText("施工");
            View SGlinearCom = itemView.findViewById(R.id.comAdd);
            SGlinearCom.setVisibility(View.VISIBLE);
            SGlinearCom.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(context, GoodsListActivity.class);
                    intent.putExtra(KEY_TITLE,"施工");
                    intent.putExtra(KEY_TYPE_GOODSID, "4");
                    context.startActivity(intent);
                    //添加施工
                }
            });
            final TextView line = (TextView) itemView.findViewById(R.id.line);
            sg_checkbox.setEnabled(false);
            QuickAdapter SGAdapter = new QuickAdapter<ProgramBean.Data.ServiceListBean>(context, R.layout.item_program_nopic_n, sgData) {
                @Override
                protected void convert(final BaseAdapterHelper helper, final ProgramBean.Data.ServiceListBean item) {
                    helper.setText(R.id.sg_name, item.getServiceName());
                    helper.getView(R.id.txtCount).setVisibility(View.GONE);
                    helper.setText(R.id.txtPrice, String.format("¥ %.2f" , Utils.toFloat(item.getServicePrice())));
                    ImageView inImageView = helper.getView(R.id.iv_pic);
                    LinearLayout isfirst = helper.getView(R.id.isFirst);
                    LinearLayout issecond = helper.getView(R.id.isSecond);
                    isfirst.setVisibility(View. VISIBLE);
                    issecond.setVisibility(View.VISIBLE);
                    final TextView tv_number = helper.getView(R.id.choice_number);
                    tv_number.setText(String.valueOf(item.getServiceNum()));
                    String serviceSwich = "" + item.getIsRecommend();
                    switch (serviceSwich) {
                        case "0":
                            inImageView.setBackgroundResource(R.drawable.i_unselect_gray_circle);
                            sg_checkbox.setChecked(false);
                            for (int i = 0; i < ProgramProjectAdapter.this.data.getServiceList().size(); i++) {
                                ProgramProjectAdapter.this.data.getServiceList().get(i).setIsRecommend(0);
                            }
                            break;
                        case "1":
                            inImageView.setBackgroundResource(R.drawable.i_select_gray_circle);
                            sg_checkbox.setChecked(true);
                            for (int i = 0; i < ProgramProjectAdapter.this.data.getServiceList().size(); i++) {
                                ProgramProjectAdapter.this.data.getServiceList().get(i).setIsRecommend(1);
                            }
                            break;
                    }
                    if (ProgramPriceView.SWITCH_EDIT.equals(sivisible_swich)||ProgramPriceView.SWITCH_SELECT_NONE.equals(sivisible_swich)||ProgramPriceView.SWITCH_SELECT_ALL.equals(sivisible_swich)) {
                        sg_checkbox.setEnabled(true);
                    } else if (ProgramPriceView.SWITCH_NO_EDIT.equals(sivisible_swich)) {

                    }
                    //减法
                    helper.getView(R.id.number_reduce).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            int n = Integer.valueOf(tv_number.getText().toString());
                            if (n > 1) {
                                n--;
                                tv_number.setText(String.valueOf(n));
                                ProgramProjectAdapter.this.data.getServiceList().get(helper.getPosition()).setServiceNum(Integer.parseInt(tv_number.getText().toString()));
                                ProgramProjectAdapter.this.data.getServiceList().get(helper.getPosition())
                                        .setServiceCount( String.valueOf(Float.valueOf(ProgramProjectAdapter.this.data.getServiceList().get(helper.getPosition()).getServicePrice()) * Integer.parseInt(tv_number.getText().toString())));
                                TotalMoney();
                            } else {
                                Utils.showToast(context, "商品数量不能小于1");
                            }

                        }
                    });
                    //加法
                    helper.getView(R.id.number_add).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            int n = Integer.valueOf(tv_number.getText().toString());
                            n++;
                            tv_number.setText(String.valueOf(n));
                            ProgramProjectAdapter.this.data.getServiceList().get(helper.getPosition()).setServiceNum(Integer.parseInt(tv_number.getText().toString()));
                            ProgramProjectAdapter.this.data.getServiceList().get(helper.getPosition())
                                    .setServiceCount( String.valueOf(Float.valueOf(ProgramProjectAdapter.this.data.getServiceList().get(helper.getPosition()).getServicePrice()) * Integer.parseInt(tv_number.getText().toString())));
                            TotalMoney();
                        }
                    });
                    //全选
                    sg_checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                        @Override
                        public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                            if (isChecked == false) {
                                sg_checkbox.setChecked(false);
                                for (int i = 0; i < ProgramProjectAdapter.this.data.getServiceList().size(); i++) {
                                    ProgramProjectAdapter.this.data.getServiceList().get(i).setIsRecommend(0);
                                }
                            } else {
                                sg_checkbox.setChecked(true);
                                for (int i = 0; i < ProgramProjectAdapter.this.data.getServiceList().size(); i++) {
                                    ProgramProjectAdapter.this.data.getServiceList().get(i).setIsRecommend(1);
                                }

                            }
                            TotalMoney();
                            notifyDataSetChanged();
                        }
                    });
                }
            };
            if (sgData.size()==0){
                sg_checkbox.setBackgroundResource(R.drawable.i_unselect_gray_circle);
            }
            TotalMoney();
            goodListView.setAdapter(SGAdapter);
        }


        //家居
        private void initGoods(View itemView, final int index, final String title, final List<ProgramBean.Data.GoodsListBean> jjData) {
            final CheckBox checkbox = (CheckBox) itemView.findViewById(R.id.iv_titleIcon);
            final NoRollSwipeMenuListView listView = (NoRollSwipeMenuListView) itemView.findViewById(R.id.SG_list);
            TextView txtGoodsCount = (TextView) itemView.findViewById(R.id.comName);
            View panel = itemView.findViewById(R.id.comAdd);
            final TextView line = (TextView) itemView.findViewById(R.id.line);
            panel.setVisibility(View.VISIBLE);
            txtGoodsCount.setText(title);
            //增项
            panel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //添加家居项目
                    Intent intent = new Intent(context, GoodsListActivity.class);
                    intent.putExtra(KEY_TYPE_GOODSID, String.valueOf(index + 1));
                    intent.putExtra(KEY_TITLE,title);
                    context.startActivity(intent);
                }
            });
            checkbox.setEnabled(false);
            final QuickAdapter adapter = new QuickAdapter<ProgramBean.Data.GoodsListBean>(context, R.layout.item_program_nopic_y, jjData) {
                @Override
                protected void convert(final BaseAdapterHelper helper, final ProgramBean.Data.GoodsListBean item) {
                    helper.setText(R.id.txtMainName, item.getGoodsName());
                    helper.setText(R.id.txtMainPrice, String.format("¥ %.2f" , Utils.toFloat(item.getGoodsPrice())));
                    TagGroup tagGroup = helper.getView(R.id.txtName);
                    tagGroup.setTags(item.getBrandType());
                    final TextView tv_number = helper.getView(R.id.choice_number);
                    tv_number.setText(String.valueOf(item.getGoodsNum()));
                    ImageView nearby_shop_img = helper.getView(R.id.iv_pic);
                    Picasso.with(context).load(item.getImageUrl()).placeholder(RES_ID_GOODS_DEFAULT).into(nearby_shop_img);
                    final CheckBox iv_icon = helper.getView(R.id.iv_icon);
                    int isRecommend = item.getIsRecommend();
                    LinearLayout isFirst = helper.getView(R.id.isFirst);
                    LinearLayout isSecond = helper.getView(R.id.isSecond);
                    isFirst.setVisibility(View.GONE);
                    isSecond.setVisibility(View.VISIBLE);

                    switch (isRecommend) {
                        case 0:
                            iv_icon.setChecked(false);
                            break;
                        case 1:
                            iv_icon.setChecked(true);
                            checkbox.setBackgroundResource(R.drawable.i_radio_on);
                            break;
                    }

                    if (ProgramPriceView.SWITCH_EDIT.equals(sivisible_swich)  ||
                            ProgramPriceView.SWITCH_SELECT_NONE.equals(sivisible_swich) ||
                            ProgramPriceView.SWITCH_SELECT_ALL.equals(sivisible_swich)) {
                        checkbox.setEnabled(true);
                        helper.setText(R.id.choice_number, "" + item.getGoodsNum());
                        notifyDataSetChanged();
                    }
                    if (ProgramPriceView.SWITCH_SELECT_NONE.equals(sivisible_swich)){
                        checkbox.setBackgroundResource(R.drawable.i_radio_off);
                    }
                    //点击标题全选不全选
                    checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                        @Override
                        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                            if (b == false) {
                                checkbox.setChecked(false);
                                switch (index){
                                    case 0:
                                        for (int i = 0; i < ProgramProjectAdapter.this.data.getMaterialList().size(); i++) {
                                            ProgramProjectAdapter.this.data.getMaterialList().get(i).setIsRecommend(0);
                                        }
                                        break;
                                    case 1:
                                        for (int i = 0; i < ProgramProjectAdapter.this.data.getFurnitureList().size(); i++) {
                                            ProgramProjectAdapter.this.data.getFurnitureList().get(i).setIsRecommend(0);
                                        }
                                        break;
                                    case 2:
                                        for (int i = 0; i < ProgramProjectAdapter.this.data.getAccessoriesList().size(); i++) {
                                            ProgramProjectAdapter.this.data.getAccessoriesList().get(i).setIsRecommend(0);
                                        }
                                        break;
                                }

                            } else {
                                checkbox.setChecked(true);
                                switch (index){
                                    case 0:
                                        for (int i = 0; i < ProgramProjectAdapter.this.data.getMaterialList().size(); i++) {
                                            ProgramProjectAdapter.this.data.getMaterialList().get(i).setIsRecommend(1);
                                        }
                                        break;
                                    case 1:
                                        for (int i = 0; i < ProgramProjectAdapter.this.data.getFurnitureList().size(); i++) {
                                            ProgramProjectAdapter.this.data.getFurnitureList().get(i).setIsRecommend(1);
                                        }
                                        break;
                                    case 2:
                                        for (int i = 0; i < ProgramProjectAdapter.this.data.getAccessoriesList().size(); i++) {
                                            ProgramProjectAdapter.this.data.getAccessoriesList().get(i).setIsRecommend(1);
                                        }
                                        break;
                                }

                            }
                            TotalMoney();
                            notifyDataSetChanged();
                        }
                    });
                    //减法
                    helper.getView(R.id.number_reduce).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            int n = Integer.valueOf(tv_number.getText().toString());
                            if (n > 1) {
                                n--;
                                tv_number.setText(String.valueOf(n));
                                switch (index){
                                    case 0:
                                        ProgramProjectAdapter.this.data.getMaterialList().get(helper.getPosition()).setGoodsNum(Integer.parseInt(tv_number.getText().toString()));
                                        ProgramProjectAdapter.this.data.getMaterialList().get(helper.getPosition())
                                                .setGoodsCount( String.valueOf(Float.valueOf(ProgramProjectAdapter.this.data.getMaterialList().get(helper.getPosition()).getGoodsPrice()) * Integer.parseInt(tv_number.getText().toString())));
                                        break;
                                    case 1:
                                        ProgramProjectAdapter.this.data.getFurnitureList().get(helper.getPosition()).setGoodsNum(Integer.parseInt(tv_number.getText().toString()));
                                        ProgramProjectAdapter.this.data.getFurnitureList().get(helper.getPosition())
                                                .setGoodsCount( String.valueOf(Float.valueOf(ProgramProjectAdapter.this.data.getFurnitureList().get(helper.getPosition()).getGoodsPrice()) * Integer.parseInt(tv_number.getText().toString())));
                                        break;
                                    case 2:
                                        ProgramProjectAdapter.this.data.getAccessoriesList().get(helper.getPosition()).setGoodsNum(Integer.parseInt(tv_number.getText().toString()));
                                        ProgramProjectAdapter.this.data.getAccessoriesList().get(helper.getPosition())
                                                .setGoodsCount( String.valueOf(Float.valueOf(ProgramProjectAdapter.this.data.getAccessoriesList().get(helper.getPosition()).getGoodsPrice()) * Integer.parseInt(tv_number.getText().toString())));
                                        break;
                                }

                                TotalMoney();
                            } else {
                                Utils.showToast(context, "商品数量不能小于1");
                            }

                        }
                    });
                    //加法
                    helper.getView(R.id.number_add).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            int n = Integer.valueOf(tv_number.getText().toString());
                            n++;
                            tv_number.setText(String.valueOf(n));
                            switch (index){
                                case 0:
                                    ProgramProjectAdapter.this.data.getMaterialList().get(helper.getPosition()).setGoodsNum(Integer.parseInt(tv_number.getText().toString()));
                                    ProgramProjectAdapter.this.data.getMaterialList().get(helper.getPosition())
                                            .setGoodsCount( String.valueOf(Float.valueOf(ProgramProjectAdapter.this.data.getMaterialList().get(helper.getPosition()).getGoodsPrice()) * Integer.parseInt(tv_number.getText().toString())));
                                    break;
                                case 1:
                                    ProgramProjectAdapter.this.data.getFurnitureList().get(helper.getPosition()).setGoodsNum(Integer.parseInt(tv_number.getText().toString()));
                                    ProgramProjectAdapter.this.data.getFurnitureList().get(helper.getPosition())
                                            .setGoodsCount( String.valueOf(Float.valueOf(ProgramProjectAdapter.this.data.getFurnitureList().get(helper.getPosition()).getGoodsPrice()) * Integer.parseInt(tv_number.getText().toString())));
                                    break;
                                case 2:
                                    ProgramProjectAdapter.this.data.getAccessoriesList().get(helper.getPosition()).setGoodsNum(Integer.parseInt(tv_number.getText().toString()));
                                    ProgramProjectAdapter.this.data.getAccessoriesList().get(helper.getPosition())
                                            .setGoodsCount( String.valueOf(Float.valueOf(ProgramProjectAdapter.this.data.getAccessoriesList().get(helper.getPosition()).getGoodsPrice()) * Integer.parseInt(tv_number.getText().toString())));
                                    break;
                            }

                            TotalMoney();
                        }
                    });

                    iv_icon.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                        @Override
                        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                            int count = 0;
                            if (b == false) {
                                iv_icon.setChecked(false);
                                switch (index){
                                    case 0:
                                        for (int i = 0; i < ProgramProjectAdapter.this.data.getMaterialList().size(); i++) {
                                            ProgramProjectAdapter.this.data.getMaterialList().get(helper.getPosition()).setIsRecommend(0);
                                            count += ProgramProjectAdapter.this.data.getMaterialList().get(i).getIsRecommend();
                                        }
                                        break;
                                    case 1:
                                        for (int i = 0; i < ProgramProjectAdapter.this.data.getFurnitureList().size(); i++) {
                                            ProgramProjectAdapter.this.data.getFurnitureList().get(helper.getPosition()).setIsRecommend(0);
                                            count += ProgramProjectAdapter.this.data.getFurnitureList().get(i).getIsRecommend();
                                        }
                                        break;
                                    case 2:
                                        for (int i = 0; i < ProgramProjectAdapter.this.data.getAccessoriesList().size(); i++) {
                                            ProgramProjectAdapter.this.data.getAccessoriesList().get(helper.getPosition()).setIsRecommend(0);
                                            count += ProgramProjectAdapter.this.data.getAccessoriesList().get(i).getIsRecommend();
                                        }
                                        break;
                                }

                                if (count == 0) {
                                    checkbox.setBackgroundResource(R.drawable.i_radio_off);
                                    checkbox.setChecked(false);
                                }
                            } else {
                                iv_icon.setChecked(true);
                                switch (index){
                                    case 0:
                                        for (int i = 0; i < ProgramProjectAdapter.this.data.getMaterialList().size(); i++) {
                                            ProgramProjectAdapter.this.data.getMaterialList().get(helper.getPosition()).setIsRecommend(1);
                                            count += ProgramProjectAdapter.this.data.getMaterialList().get(i).getIsRecommend();
                                        }
                                        if (count == ProgramProjectAdapter.this.data.getMaterialList().size()) {
                                            checkbox.setChecked(true);
                                        }
                                        break;
                                    case 1:
                                        for (int i = 0; i < ProgramProjectAdapter.this.data.getFurnitureList().size(); i++) {
                                            ProgramProjectAdapter.this.data.getFurnitureList().get(helper.getPosition()).setIsRecommend(1);
                                            count += ProgramProjectAdapter.this.data.getFurnitureList().get(i).getIsRecommend();
                                        }
                                        if (count == ProgramProjectAdapter.this.data.getFurnitureList().size()) {
                                            checkbox.setChecked(true);
                                        }
                                        break;
                                    case 2:
                                        for (int i = 0; i < ProgramProjectAdapter.this.data.getAccessoriesList().size(); i++) {
                                            ProgramProjectAdapter.this.data.getAccessoriesList().get(helper.getPosition()).setIsRecommend(1);
                                            count += ProgramProjectAdapter.this.data.getAccessoriesList().get(i).getIsRecommend();
                                        }
                                        if (count == ProgramProjectAdapter.this.data.getAccessoriesList().size()) {
                                            checkbox.setChecked(true);
                                        }
                                        break;
                                }

                                if (count > 0) {
                                    checkbox.setBackgroundResource(R.drawable.i_radio_on);
                                }

                            }

                            TotalMoney();
                        }
                    });

                }
            };
            if (jjData.size()==0){
                checkbox.setBackgroundResource(R.drawable.i_unselect_gray_circle);
            }
            TotalMoney();

            List<ProgramBean.Data.GoodsListBean> showList = new ArrayList<>();
            final TextView tvMore = (TextView) itemView.findViewById(R.id.tvMore);
            if (jjData != null){
                if (jjData.size() > SHOW_SIZE){
                    Object objSize = tvMore.getTag(R.id.decode);
                    if (objSize != null && objSize instanceof Integer){
                        int type = (int) objSize;
                        if (type == 1){
                            showList.addAll(jjData.subList(0,SHOW_SIZE));
                        }else{
                            showList.addAll(jjData);
                        }
                    }else{
                        showList.addAll(jjData.subList(0,SHOW_SIZE));
                        tvMore.setVisibility(View.VISIBLE);
                        tvMore.setTag(R.id.decode,1);
                    }

                    tvMore.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Object tag = view.getTag(R.id.decode);
                            if (tag != null && tag instanceof Integer){
                                int type = (int) tag;
                                if (type == 1){
                                    tvMore.setTag(R.id.decode,2);
                                    tvMore.setText("收起列表");
                                    adapter.clear();
                                    adapter.addAll(jjData);
                                }else{
                                    tvMore.setTag(R.id.decode,1);
                                    tvMore.setText("查看全部");
                                    adapter.clear();
                                    adapter.addAll(jjData.subList(0,SHOW_SIZE));
                                }
                            }
                        }
                    });
                }else{
                    showList.addAll(jjData);
                    tvMore.setVisibility(View.GONE);
                }

                adapter.clear();
                adapter.addAll(showList);
            }
            listView.setAdapter(adapter);
        }

    }

    public void TotalMoney() {
        float sgPrice = 0;
        float zcPrice = 0;
        float jjPrice = 0;
        float psPrice = 0;
        List<ProgramBean.Data.ServiceListBean> serviceListBeen = new ArrayList<>();
        List<ProgramBean.Data.GoodsListBean> materialListBeen = new ArrayList<>();
        List<ProgramBean.Data.GoodsListBean> furnitureListBeen = new ArrayList<>();
        List<ProgramBean.Data.GoodsListBean> accessoriesListBeen = new ArrayList<>();
        //施工总金额
        if (ProgramProjectAdapter.this.data.getServiceList() != null) {
            for (int i = 0; i < ProgramProjectAdapter.this.data.getServiceList().size(); i++) {
                if (ProgramProjectAdapter.this.data.getServiceList().get(i).getIsRecommend() == 1) {
                    sgPrice += Float.parseFloat(ProgramProjectAdapter.this.data.getServiceList().get(i).getServicePrice()) *
                            Float.valueOf(ProgramProjectAdapter.this.data.getServiceList().get(i).getServiceNum());
                    serviceListBeen.add(ProgramProjectAdapter.this.data.getServiceList().get(i));
                }
            }
        }
        //资材总金额
        if (ProgramProjectAdapter.this.data.getMaterialList() != null) {
            for (int i = 0; i < ProgramProjectAdapter.this.data.getMaterialList().size(); i++) {
                if (ProgramProjectAdapter.this.data.getMaterialList().get(i).getIsRecommend() == 1) {
                    zcPrice += Float.parseFloat(ProgramProjectAdapter.this.data.getMaterialList().get(i).getGoodsPrice()) * Float.valueOf(ProgramProjectAdapter.this.data.getMaterialList().get(i).getGoodsNum());
                    materialListBeen.add(ProgramProjectAdapter.this.data.getMaterialList().get(i));
                }
            }
        }
        //家居总金额
        if (ProgramProjectAdapter.this.data.getFurnitureList() != null) {
            for (int i = 0; i < ProgramProjectAdapter.this.data.getFurnitureList().size(); i++) {
                if (ProgramProjectAdapter.this.data.getFurnitureList().get(i).getIsRecommend() == 1) {
                    jjPrice += Float.parseFloat(ProgramProjectAdapter.this.data.getFurnitureList().get(i).getGoodsPrice()) * Float.valueOf(ProgramProjectAdapter.this.data.getFurnitureList().get(i).getGoodsNum());
                    furnitureListBeen.add(ProgramProjectAdapter.this.data.getFurnitureList().get(i));
                }
            }
        }
        //配飾总金额
        if (ProgramProjectAdapter.this.data.getAccessoriesList() != null) {
            for (int i = 0; i < ProgramProjectAdapter.this.data.getAccessoriesList().size(); i++) {
                if (ProgramProjectAdapter.this.data.getAccessoriesList().get(i).getIsRecommend() == 1) {
                    psPrice += Float.parseFloat(ProgramProjectAdapter.this.data.getAccessoriesList().get(i).getGoodsPrice()) * Float.valueOf(ProgramProjectAdapter.this.data.getAccessoriesList().get(i).getGoodsNum());
                    accessoriesListBeen.add(ProgramProjectAdapter.this.data.getAccessoriesList().get(i));
                }
            }
        }
        int count = accessoriesListBeen.size() + furnitureListBeen.size() + materialListBeen.size() + serviceListBeen.size();
        int totalCount = data.getServiceList().size() + data.getAccessoriesList().size() + data.getFurnitureList().size() + data.getMaterialList().size();
        if (count > 0 && count == totalCount) {
            Object[] all = new Object[2];
            all[0] = CALLBACK_TYPE_BOOLEAN;
            all[1] = true;
            loadListener.onLoad(all);

        } else {
            Object[] noall = new Object[2];
            noall[0] = CALLBACK_TYPE_BOOLEAN;
            noall[1] = false;
            loadListener.onLoad(noall);
        }
        Object[] objects = new Object[2];
        objects[0] = CALLBACK_TYPE_QUICK_ADAPTER;
        objects[1] = sgPrice + zcPrice + jjPrice + psPrice;
        loadListener.onLoad(objects);
    }

}
